﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace Library
{

    public partial class InstanceViewForm : Form
    
    {
        LibraryEntities1 ins = new LibraryEntities1();
        public static ObjectSet<InstanceBook> instanceBooks;
        public InstanceBook instance =  new InstanceBook();
        DataGridViewRowCollection rows;
        bool add = false;
        public InstanceViewForm(InstanceBook instance, bool add, DataGridViewRowCollection rows)
        {
            InitializeComponent();
            instanceBooks = ins.InstanceBook;
            this.instance = instance;
            this.add = add;
            this.rows = rows;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (instance == null)
                instance = new InstanceBook();
            instance.InventoryNumber = textBox1.Text.Trim();
            instance.Control = radioButton1.Checked;
            bool allowToAdd = true;
            foreach (DataGridViewRow x in rows)
            {
                if (x.Cells[1].Value.ToString().Trim().ToUpper() == instance.InventoryNumber.ToUpper())
                {
                    MessageBox.Show("Книга с таким инвентарным номером уже существует");
                    allowToAdd = false;
                }
            }
            if (allowToAdd)
            {
                if (instanceBooks.Where(x=>x.InventoryNumber.ToUpper() == instance.InventoryNumber.ToUpper()).FirstOrDefault() != null)
                    MessageBox.Show("Книга с таким инвентарным номером уже существует");
                else
                    this.Close();
            }
                
        }

        private void InstanceViewForm_Load(object sender, EventArgs e)
        {
            if (instance != null)
            {
                textBox1.Text = instance.InventoryNumber;
                if (Convert.ToBoolean(instance.Control))
                    radioButton1.Checked = true;
            }
        }
    }
}
